#include "../source/socket.h"
#include "../source/io_manager.h"
#include "../source/log.h"

void test()
{
    IPAddress::ptr addr = Address::LookupAnyIPAddress("www.baidu.com:80");
    if(addr)
    {
        log_info("get addr: {}", addr->toString());
    }
    else
    {
        log_error("ger addr error");
        return;
    }

    Socket::ptr sock = Socket::CreateTCP(addr);
    // addr->setPort(80);
    log_debug("addr: {}", addr->toString());
    if(!sock->connect(addr))
    {
        log_error("connect error");
        return;
    }

    const char buff[] = "GET / HTTP/1.0\r\n\r\n";
    int ret = sock->send(buff, sizeof(buff));
    if(ret <= 0)
    {
        log_error("send fail rt:{}", ret);
        return;
    }

    std::string buffer;
    buffer.resize(8192*3);
    log_debug("==============================================================");
    ret = sock->recv(&buffer[0], buffer.size());
    log_debug("{}", buffer);
    
    log_debug("==============================================================");
    log_debug("sock info: {}", sock->toString()); 
}

int main(void)
{
    IOManager iom;
    iom.schedule(test);

    return 0;
}